﻿using CaroLibraly;
using System;
using System.Windows.Forms;
using ThuVienWinform.Mang;

namespace CaroClient
{
    public partial class frmConnect : Form
    {
        //Hiển thị danh sách địa chỉ IP, tên của máy này
        private void LoadPlayerInfomation()
        {
            this.txtName.Text = CaroClient.Properties.Settings.Default.PlayerName;
            if (CaroClient.Properties.Settings.Default.PlayerName == "")
                this.txtName.Text = Mang.TenMay;
            this.txtIpServer.Text = CaroClient.Properties.Settings.Default.ServerIp;
            foreach (string ip in Mang.DanhSachIpCaNhan)
                this.cbbIp.Items.Add(ip);
            this.cbbIp.Items.Add("Làm mới...");
            this.cbbIp.SelectedIndex = 0;
        }

        public frmConnect()
        {
            InitializeComponent();
        }

        private void frmConnect_Load(object sender, EventArgs e)
        {
            this.LoadPlayerInfomation();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Global.Connect(txtIpServer.Text);
            Global.Player = new Player(this.txtName.Text, this.cbbIp.Text);
            if (Global.Connected)
            {
                CaroClient.Properties.Settings.Default.ServerIp = this.txtIpServer.Text;
                CaroClient.Properties.Settings.Default.PlayerName = this.txtName.Text;
                Global.Server.JoinServer(Global.Player);
                CaroClient.Properties.Settings.Default.Save();
            }
            else
            {
                this.lblStatus.Text = "Không kết nối được máy chủ!";
            }
        }

        private void cbbIp_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbbIp.Text == "Làm mới...")
            {
                this.cbbIp.Items.Clear();
                foreach (string ip in Mang.DanhSachIpCaNhan)
                    this.cbbIp.Items.Add(ip);
                this.cbbIp.Items.Add("Làm mới...");
                this.cbbIp.SelectedIndex = 0;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
